package com.diandi.messages.util;

import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 
 * 阿里大鱼短信接口
 * 
 * @author liuxu
 * @since 2016年7月11日
 *
 */
@Component
public class AliBigFishUtil {
	private static final Logger logger = LoggerFactory.getLogger(AliBigFishUtil.class);

	private static String url;
	private static String appKey;
	private static String secert;
	private static Properties properties = new Properties();

	/**
	 * 系统加载
	 */
	@PostConstruct
	private void initSys() {
		try {
			InputStream inputStream = SmsUtils.class.getClassLoader().getResourceAsStream("sms.properties");
			properties.load(inputStream);
			url = properties.getProperty("ALI_BIG_FISH_URL");
			appKey = properties.getProperty("ALI_BIG_FISH_APPKEY");
			secert = properties.getProperty("ALI_BIG_FISH_SECERT");

		} catch (FileNotFoundException e) {
			logger.error("文件不存在！sms.properties");
		} catch (IOException e) {
			logger.error("文件sms.properties，读取错误");
		} catch (Exception e) {
			logger.error("获取属性异常：{}", e.getMessage());
		}
	}

	public static Map<String,String> sendMsg(AliSMSDto dto) {

		Map<String,String> rtnMap = new HashMap<String, String>() ;
		TaobaoClient client = new DefaultTaobaoClient(url, appKey, secert);

		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType("normal");
		req.setSmsFreeSignName(dto.getSmsFreeSignName());
		req.setSmsParam(JSON.toJSONString(dto.getSmsParam()));
		req.setRecNum(dto.getMobilePhone());
		req.setSmsTemplateCode(dto.getTemplateCode());
		AlibabaAliqinFcSmsNumSendResponse response = new AlibabaAliqinFcSmsNumSendResponse();

		try {
			response = client.execute(req);
			System.out.println();
			if (true == response.getResult().getSuccess()) {
				rtnMap.put( "code","0000" );
				rtnMap.put( "msg","短信发送成功" );
			}else{
				rtnMap.put( "code","1000" );
				rtnMap.put( "msg",response.getErrorCode()+response.getMsg() );
			}
		} catch (ApiException e) {
			rtnMap.put( "code","1000" );
			rtnMap.put( "msg",e.getErrMsg().toString());
		} finally {
			return rtnMap;
		}
	}

	@SuppressWarnings("finally")
	public static Map<String,String> sendMsg ( Map<String,String> paramMap ) {

		Map<String,String> rtnMap = new HashMap<String, String>() ;
		TaobaoClient client = new DefaultTaobaoClient(url, appKey, secert);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType("normal");
		req.setSmsFreeSignName(paramMap.get("msgTest"));
		req.setSmsParam(JSON.toJSONString(paramMap.get("msgTest").toString()));
		req.setRecNum( paramMap.get(""));
		req.setSmsTemplateCode( paramMap.get("") );
		AlibabaAliqinFcSmsNumSendResponse response = new AlibabaAliqinFcSmsNumSendResponse();

		try {
			response = client.execute( req );
			if (true == response.getResult().getSuccess()) {
				rtnMap.put( "code","0000" );
				rtnMap.put( "msg","短信发送成功" );
			}
		} catch (ApiException e) {
			rtnMap.put( "code","1000" );
			rtnMap.put( "msg",e.getErrMsg().toString());
		} finally {
			return rtnMap;
		}
	}

	private static boolean validateParam(AliSMSDto dto) {
		if (null == dto) {
			return false;
		}

		if (StringUtils.isBlank(dto.getMobilePhone()) || StringUtils.isBlank(dto.getSmsFreeSignName())
				|| null == dto.getSmsParam() || StringUtils.isBlank(dto.getTemplateCode())) {
			return false;
		}

		return true;
	}
}
